<template>
  <div class="nbox1">
    <div class="select_gap">
      <div class="__nav gap1">
        <n-select
          :options="sval.season_list"
          :value="sval.season_click"
          @update:value="set.sl_click"
        />
      </div>
      <div class="flex g8">
        <n-button @click="set.back">返回公会列表</n-button>
        <n-button @click="set.back">更新公会数据</n-button>
      </div>
    </div>
  </div>
</template>
<style lang="css" scoped>
.select_gap .__nav {
  display: flex;
  justify-content: center;
  align-items: center;
}

@media (max-width: 700px) {
  .select_gap .__nav {
    grid-column: span 2;
  }
}
</style>
<script>
import { cfg } from "@/cfg/cfg.js";

import { getrole } from "@/cfg/function.js";
export default {
  props: {
    tjval: {
      type: Object,
      required: false,
    },
  },
  setup(props) {
    const sval = props.tjval;
    const set = {
      check_guilddata: async () => {
        try {
          const gd = sval.guild_data;
          if (gd.type == "old") {
            return;
          } else {
            const back = await cfg.mget({
              url: `${cfg.url2}api/guild`,
              query: {
                type: "check_guild",
                mcdmg: gd.mcdmg,
                guild_id: cfg.guild_id,
              },
            });
            if (back.code == 0) {
              const d = back.data;
              if (d.upd == true) {
                cfg
                  .alert2({
                    title: "公会更新提醒",
                    message: "公会有新数据，是否重载公会数据",
                  })
                  .then(() => {
                    //更新公会
                  })
                  .catch(() => {});
              }
            } else {
              throw { code: -1, message: back.msg };
            }
          }
        } catch (error) {
          cfg.alert3({
            message: `${error.message}`,
          });
        }
      },
      load_user: async () => {
        const d = sval.guild_data;
        const u = d.user_arr;
        const conf = d.conf;
        d.user = {};
        //如果已经加载过，则直接返回true
        if (conf.user == true) {
          return true;
        }
        //如果没有加载过，则加载
        if (u?.length >= 1) {
          for (let index = 0; index < u.length; index++) {
            const d2 = u[index];
            d.user[d2.user_id] = d2;
            d.user[d2.user_name] = d2;
          }
          //加载完成，设置conf.user为true
          conf.user = true;
        }
        return true;
      },
      get_guild: async () => {
        try {
          const back = await cfg.mget({
            url: `${cfg.url2}api/guild`,
            query: {
              type: "get_guild_data",
              guild_id: cfg.guild_id,
              season: sval.season_click,
            },
          });
          if (back.code == 0) {
            sval.guild_data = back.data;
            sval.guild_data.status = "ok";
            sval.guild_data.set = set;
            sval.guild_data.conf = {};
            sval.guild_data.maodate = {};
            const date = sval.guild_data.date;
            for (let index = 0; index < date.length; index++) {
              const element = date[index];
              sval.guild_data.maodate[element] = index;
            }
          } else {
            sval.guild_data.status = "error";
            throw { code: -1, message: back.msg };
          }
        } catch (e) {
          cfg.alert3({
            message: e.message,
            closeOnClickOverlay: true,
          });
        }
      },
      back: () => {
        cfg.guild_id = null;
        sval.guild_data = {};
        localStorage.removeItem("guild_id");
        localStorage.removeItem("guild_gid");
        cfg.router.replace({
          params: {
            guild_id: "",
          },
        });
      },
      sl_click: async (sid) => {
        sval.season_click = sid;
        cfg.router.replace({
          params: {
            season: sid,
          },
        });
        sval.loading = true;
        await set.get_guild();
        sval.loading = false;
      },
    };

    const val = reactive({});
    onMounted(async () => {
      sval.loading = true;
      await getrole();
      await set.get_guild();
      sval.loading = false;
    });
    return { sval, cfg, set, val };
  },
};
</script>
